﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Globalization;

namespace FancyIdea.PingShui.Core.Util
{
    public class StringParser
    {
        private readonly static DateTimeFormatInfo DefaultDateTimeFormat
            = new DateTimeFormatInfo();

        static StringParser()
        {
            DefaultDateTimeFormat.SetAllDateTimePatterns(new string[] {
                "yyyy-MM-dd", "yyyy-M-d", "yyyy/MM/dd", "yyyy/M/d"
            }, 'd');
            DefaultDateTimeFormat.SetAllDateTimePatterns(new string[] {
                "HH:mm:ss", "H:m:s"
            }, 't');
        }

        #region ToInt32
        public static int? ToInt32(string value, NumberStyles styles)
        {
            try
            {
                return int.Parse(value, styles);
            }
            catch
            {
                return null;
            }
        }

        public static int? ToInt32(string value)
        {
            return ToInt32(value, NumberStyles.Integer);
        }

        public static int ToInt32(string value, NumberStyles styles,
            int defaultValue)
        {
            int? r = ToInt32(value, styles);
            return r == null ? defaultValue : r.Value;
        }

        public static int ToInt32(string value, int defaultValue)
        {
            int? r = ToInt32(value, NumberStyles.Integer, defaultValue);
            return r == null ? defaultValue : r.Value;
        }
        #endregion

        #region ToInt16
        public static short? ToInt16(string value, NumberStyles styles)
        {
            try
            {
                return short.Parse(value, styles);
            }
            catch
            {
                return null;
            }
        }

        public static short? ToInt16(string value)
        {
            return ToInt16(value, NumberStyles.Integer);
        }

        public static short ToInt16(string value, NumberStyles styles,
            short defaultValue)
        {
            short? r = ToInt16(value, styles);
            return r == null ? defaultValue : r.Value;
        }

        public static short ToInt16(string value, short defaultValue)
        {
            short? r = ToInt16(value, NumberStyles.Integer, defaultValue);
            return r == null ? defaultValue : r.Value;
        }
        #endregion

        #region ToInt64
        public static long? ToInt64(string value, NumberStyles styles)
        {
            try
            {
                return long.Parse(value, styles);
            }
            catch
            {
                return null;
            }
        }

        public static long? ToInt64(string value)
        {
            return ToInt64(value, NumberStyles.Integer);
        }

        public static long ToInt64(string value, NumberStyles styles,
            long defaultValue)
        {
            long? r = ToInt64(value, styles);
            return r == null ? defaultValue : r.Value;
        }

        public static long ToInt64(string value, long defaultValue)
        {
            long? r = ToInt64(value, NumberStyles.Integer, defaultValue);
            return r == null ? defaultValue : r.Value;
        }
        #endregion

        #region ToBoolean
        public static bool? ToNullableBoolean(string value)
        {
            try
            {
                return bool.Parse(value);
            }
            catch
            {
                return null;
            }
        }

        public static bool ToBoolean(string value, bool defaultValue)
        {
            bool? r = ToNullableBoolean(value);
            return r == null ? defaultValue : r.Value;
        }

        public static bool ToBoolean(string value)
        {
            return ToBoolean(value, false);
        }
        #endregion

        #region ToDateTime
        public static DateTime? ToDateTime(string value)
        {
            try
            {
                return DateTime.Parse(value, DefaultDateTimeFormat);
            }
            catch
            {
                return null;
            }
        }

        public static DateTime ToDateTime(string value, DateTime defaultValue)
        {
            DateTime? r = ToDateTime(value);
            return r == null ? defaultValue : r.Value;
        }
        #endregion

        #region ToDecimal
        public static decimal? ToDeciaml(string value)
        {
            try
            {
                return decimal.Parse(value);
            }
            catch
            {
                return null;
            }
        }

        public static decimal ToDeciaml(string value, decimal defaultValue)
        {
            decimal? r = ToDeciaml(value);
            return r == null ? defaultValue : r.Value;
        }
        #endregion

        #region ToFloat
        public static float? ToFloat(string value)
        {
            try
            {
                return float.Parse(value);
            }
            catch
            {
                return null;
            }
        }

        public static float ToFloat(string value, float defaultValue)
        {
            float? r = ToFloat(value);
            return r == null ? defaultValue : r.Value;
        }
        #endregion

        #region ToDouble
        public static double? ToDouble(string value)
        {
            try
            {
                return double.Parse(value);
            }
            catch
            {
                return null;
            }
        }

        public static double ToDouble(string value, double defaultValue)
        {
            double? r = ToDouble(value);
            return r == null ? defaultValue : r.Value;
        }
        #endregion
    }
}